// pages/user/validate.js
import {editRemoteUserInfo, getSmsCode, validateSmsCode, removeVerifyPic} from '../../api/index'
import {isEmptyField} from '../../utils/util'
import {vModel} from '../../utils/v-model'
import {shareApp} from '../../utils/shareApp'

Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    handle: '',
    role: '1',
    codeTimer: 120, // 计时
    smsCode: '',
    codeBtnDisabled: false,
    phone: ''
  },
  // v-model
  bindInput (e) {
    vModel(this, e)
  },
  // 计时器
  setInterval () {
    let _this = this
    let _timer = setInterval(() => {
      this.setData({
        codeTimer: _this.data.codeTimer - 1
      })
    }, 1000)
    setTimeout(() => {
      clearInterval(_timer)
      this.setData({
        codeBtnDisabled: false,
        codeTimer: 60
      })
    }, 120000)
  },
  // 发送验证码
  bindSendCode () {
    let _this = this
    let _phone = _this.data.phone.trim()
    if (_phone === '') {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号'
      })
      return
    }
    if (!(/1\d{10}/.test(_phone))) {
      wx.showToast({
        icon: 'none',
        title: '手机号不正确'
      })
      return
    }
    getSmsCode(_phone).then(res => {
      console.log(res)
      _this.setInterval()
      _this.setData({
        codeBtnDisabled: true
      })
      wx.showToast({
        title: '已发送'
      })
    }).catch(err => {
      wx.showToast({
        icon: 'none',
        title: err.data.info
      })
    })
  },
  bindNextStep () {
    let _this = this
    let {role, phone, smsCode, handle} = _this.data
    if (isEmptyField(phone)) {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号'
      })
      return
    }
    
    if (isEmptyField(smsCode)) {
      wx.showToast({
        icon: 'none',
        title: '请输入验证码'
      })
      return
    }
    
    // 验证短信， 并提交信息
    validateSmsCode({phone: phone, code: smsCode}).then(res => {
      // 解绑
      removeVerifyPic().then(res => {
        wx.showToast({
          icon: 'none',
          title: res.data.info
        })
        if (handle === 'edit') {
          setTimeout(() => {
            // 修改信息
            wx.redirectTo({
              url: `/pages/init/step2?role=${role}&from=userinfo`,
            })
          }, 2000)
        } else {
          setTimeout(() => {
            wx.redirectTo({
              url: '/pages/user/userinfo'
            })
          }, 2000)
        }
      }).catch(err => {
        wx.showToast({
          icon: 'none',
          title: err.data.info
        })
      })
      
    }).catch(err => {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: err.data.info
      })
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.setData({
      handle: options.handle,
      phone: options.phone,
      role: options.role
    })
    console.log(options.handle)
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return shareApp()
  }
})
